//
//  DECXOrderDetailsViewController.m
//  DECXInStore
//
//  Created by 李坤 on 2018/7/9.
//  Copyright © 2018年 程强. All rights reserved.
//

#import "DECXOrderDetailsViewController.h"
#import "DECXManager.h"
#import "DECXStoreOrderCommitModel.h"
#import "UIView+TapEvent.h"
#import "YBImageBrowser.h"
#import "DECXDeliveryViewController.h"

@interface DECXOrderDetailsViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollWidth;
@property (weak, nonatomic) IBOutlet UILabel *orderNoLbl;
@property (weak, nonatomic) IBOutlet UILabel *markLbl;
@property (weak, nonatomic) IBOutlet UIImageView *productImage;
@property (weak, nonatomic) IBOutlet UILabel *ownerLbl;
@property (weak, nonatomic) IBOutlet UIImageView *otherImage;
@property (weak, nonatomic) IBOutlet UILabel *weightLbl;
@property (weak, nonatomic) IBOutlet UILabel *restWeightLbl;
@property (weak, nonatomic) IBOutlet UILabel *placeLbl;
@property (weak, nonatomic) IBOutlet UILabel *categoryLbl;
@property (weak, nonatomic) IBOutlet UILabel *qualityLbl;
@property (weak, nonatomic) IBOutlet UILabel *stockNoLbl;
@property (weak, nonatomic) IBOutlet UILabel *warehouceOwnerLbl;
@property (weak, nonatomic) IBOutlet UILabel *warehousePhone;
@property (weak, nonatomic) IBOutlet UILabel *warehouseTelLbl;
@property (weak, nonatomic) IBOutlet UILabel *warehouseEmailLbl;
@property (weak, nonatomic) IBOutlet UILabel *warehouseAddressLbl;
@property (weak, nonatomic) IBOutlet UILabel *warehouseStatusLbl;
@property (weak, nonatomic) IBOutlet UIView *verifyView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *verifyViewHeight;
@property (strong, nonatomic) DECXManager *manager;
@property (strong, nonatomic) DECXStoreOrderCommitModel *orderObj;
@end

@implementation DECXOrderDetailsViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
- (void)gotoBack
{
    [super gotoBack];
    if ([self.pushKey isEqualToString:@"Search"]) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollWidth.constant = ScreenWidth;
    self.scrollHeight.constant = 900;
    self.manager = [DECXManager new];
    [self fetchDetails];
}
- (void)setRightBarItem
{
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"提货" style:UIBarButtonItemStylePlain target:self action:@selector(deliveryClick)];
    self.navigationItem.rightBarButtonItem = item;
}
- (void)deliveryClick
{
    DECXDeliveryViewController *vc = StoryBoardViewController(@"Main", @"DECXDeliveryViewController");
    vc.order = self.orderObj;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)fetchDetails
{
    weak_Self(self);
    [[self.manager getOrderDetails:self.list_id]subscribeNext:^(id x) {
        weakSelf.orderObj = x;
        [weakSelf loadDetails];
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
}
- (void)loadDetails
{
    weak_Self(self);
    self.orderNoLbl.text = self.orderObj.getlist_id;
    self.markLbl.text = self.orderObj.mark.length==0?@"无":self.orderObj.mark;
    [self.productImage setImageWithURL:[NSURL URLWithString:self.orderObj.img_small] placeholderImage:LoadImage(@"place_image")];
    self.productImage.userInteractionEnabled = YES;
    [self.productImage singleClick:^{
        [weakSelf viewBigPicture:weakSelf.orderObj.img withImageView:weakSelf.productImage];
    }];
    self.ownerLbl.text = self.orderObj.user_name;
    [self.otherImage setImageWithURL:[NSURL URLWithString:self.orderObj.goods_owner_prove_small] placeholderImage:LoadImage(@"place_image")];
    self.otherImage.userInteractionEnabled = YES;
    [self.otherImage singleClick:^{
        [weakSelf viewBigPicture:weakSelf.orderObj.goods_owner_prove withImageView:weakSelf.otherImage];
    }];
    self.weightLbl.text = [NSString stringWithFormat:@"%@吨",self.orderObj.inner_weight];
    self.restWeightLbl.text = [NSString stringWithFormat:@"%@吨",self.orderObj.weight_useable];
    self.placeLbl.text = self.orderObj.goods_place;
    self.categoryLbl.text = self.orderObj.goods_type;
    self.qualityLbl.text = self.orderObj.depot_quality;
    self.stockNoLbl.text = self.orderObj.stack;
    self.warehouceOwnerLbl.text = self.orderObj.depot_responsible;
    self.warehousePhone.text = self.orderObj.responsible_mobile.length==0?@"无":self.orderObj.responsible_mobile;
    self.warehouseTelLbl.text = self.orderObj.responsible_phone.length==0?@"无":self.orderObj.responsible_phone;
    self.warehouseEmailLbl.text = self.orderObj.responsible_email.length==0?@"无":self.orderObj.responsible_email;
    self.warehouseAddressLbl.text = self.orderObj.depot_addr;

    if ([[DECXGlobal shareInstance].loginType isEqualToString:@"3"]) {
        if ([self.orderObj.state isEqualToString:@"0"]) {
            self.warehouseStatusLbl.text = @"未审核";
            self.verifyView.hidden = NO;
            self.verifyViewHeight.constant = 60;
        }else if ([self.orderObj.state isEqualToString:@"1"]){
            self.warehouseStatusLbl.text = @"审核通过";//审核通过之后可以提货
            self.verifyView.hidden = YES;
            self.verifyViewHeight.constant = 0;
            [self setRightBarItem];
        }else if ([self.orderObj.state isEqualToString:@"2"]){
            self.warehouseStatusLbl.text = @"审核未通过";
            self.verifyView.hidden = YES;
            self.verifyViewHeight.constant = 0;
        }
    }else{
        self.verifyView.hidden = YES;
        self.verifyViewHeight.constant = 0;
        if ([self.orderObj.state isEqualToString:@"0"]) {
            self.warehouseStatusLbl.text = @"未审核";
        }else if ([self.orderObj.state isEqualToString:@"1"]){
            self.warehouseStatusLbl.text = @"审核通过";
        }else if ([self.orderObj.state isEqualToString:@"2"]){
            self.warehouseStatusLbl.text = @"审核未通过";
        }
    }
}
- (IBAction)verifySuccessClick:(id)sender
{
    weak_Self(self);
    [[self.manager verifyOrder:self.list_id state:@"1"]subscribeNext:^(id x) {
        ToastShowInWindow(@"审核通过");
        [weakSelf gotoBack];
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
}
- (IBAction)rejectClick:(id)sender
{
    weak_Self(self);
    [[self.manager verifyOrder:self.list_id state:@"2"]subscribeNext:^(id x) {
        ToastShowInWindow(@"审核未通过");
        [weakSelf gotoBack];
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
}
- (void)viewBigPicture:(NSString *)url withImageView:(UIImageView *)image
{
    YBIBImageData *data = [YBIBImageData new];
    data.imageURL = [NSURL URLWithString:url];
    data.projectiveView = image;
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = @[data];
    browser.currentPage = 0;
    [browser show];
}
- (NSArray<NSString *> *)imagesURLForBrowse
{
    return @[@"http://www.decxagri.com/data/attachment/2018-05/36f6db6b-c5d0-4667-b812-b899cc583eac.png"];
}
- (CGRect)frameFormWindow
{
    return self.productImage.frame;
}
- (CGRect)frameToWindow
{
    return self.productImage.frame;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
